home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT06NEW.ZIP / TUT6.TXT < prev   
Text File  |  1995-01-04  |  10KB  |  221 lines

  1.  
  2.                    ╒═══════════════════════════════╕
  3.                    │         W E L C O M E         │
  4.                    │  To the VGA Trainer Program   │ │
  5.                    │              By               │ │
  6.                    │      DENTHOR of ASPHYXIA      │ │ │
  7.                    │      (updated by Snowman)     │ │ │
  8.                    ╘═══════════════════════════════╛ │ │
  9.                      ────────────────────────────────┘ │
  10.                        ────────────────────────────────┘
  11.  
  12.                            --==[ PART 6 ]==--
  13.  
  14.  
  15. [Note: things in brackets have been added by Snowman.  The original text
  16. has remained mostly unaltered except for the inclusion of C++ material]
  17.  
  18. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  19. ■ Introduction
  20.  
  21. Hi there! I'm back, with the latest part in the series : Pregenerated
  22. arrays. This is a fairly simple concept that can treble the speed of
  23. your code, so have a look.
  24.  
  25. I still suggest that if you haven't got a copy of TEXTER that you get it.
  26. This is shareware, written by me, that allows you to grab fonts and use
  27. them in your own programs.
  28.  
  29. I downloaded the Friendly City BBS Demo, an intro for a PE BBS, written
  30. by a new group called DamnRite, with coder Brett Step. The music was
  31. excellent, written by Kon Wilms (If I'm not mistaken, he is an Amiga
  32. weenie ;-)). A very nice first production, and I can't wait to see more
  33. of their work. I will try con a local BBS to allow me to send Brett some
  34. fido-mail.
  35.  
  36. If you would like to contact me, or the team, there are many ways you
  37. can do it : 1) Write a message to Grant Smith in private mail here on
  38.                   the Mailbox BBS.
  39.             2) Write a message here in the Programming conference here
  40.                   on the Mailbox (Preferred if you have a general
  41.                   programming query or problem others would benefit from)
  42.             3) Write to ASPHYXIA on the ASPHYXIA BBS.
  43.             4) Write to Denthor, Eze or Livewire on Connectix.
  44.             5) Write to :  Grant Smith
  45.                            P.O.Box 270 Kloof
  46.                            3640
  47.                            Natal
  48.             6) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
  49.                   call during varsity)
  50.  
  51. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  52.        to do you a demo, leave mail to me; we can discuss it.
  53. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  54.         quite lonely and want to meet/help out/exchange code with other demo
  55.         groups. What do you have to lose? Leave a message here and we can work
  56.         out how to transfer it. We really want to hear from you!
  57.  
  58.  
  59. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  60. ■  Why do I need a lookup table? What is it?
  61.  
  62. A lookup table is an imaginary table in memory where you look up the
  63. answers to certain mathematical equations instead of recalculating them
  64. each time. This may speed things up considerably. Please note that a
  65. lookup table is sometimes referred to as a pregenerated array.
  66.  
  67. One way of looking at a lookup table is as follows : Let us say that for
  68. some obscure reason you need to calculate a lot of multiplications (eg.
  69. 5*5 , 7*4 , 9*2 etc.). Instead of actually doing a slow multiply each
  70. time, you can generate a kind of bonds table, as seen below :
  71.  
  72.  
  73. ╔═╤═╦═══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╤══════╗
  74. ╟─┼─╢   1   │  2   │  3   │  4   │  5   │  6   │  7   │  8   │  9   ║
  75. ╟─┴─╫═══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╪══════╡
  76. ║ 1 ║   1   │  2   │  3   │  4   │  5   │  6   │  7   │  8   │  9   │
  77. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  78. ║ 2 ║   2   │  4   │  6   │  8   │  10  │  12  │  14  │  16  │  18  │
  79. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  80. ║ 3 ║   3   │  6   │  9   │  12  │  15  │  18  │  21  │  24  │  27  │
  81. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  82. ║ 4 ║   4   │  8   │  12  │  16  │  20  │  24  │  28  │  32  │  36  │
  83. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  84. ║ 5 ║   5   │  10  │  15  │  20  │  25  │  30  │  35  │  40  │  45  │
  85. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  86. ║ 6 ║   6   │  12  │  18  │  24  │  30  │  36  │  42  │  48  │  54  │
  87. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  88. ║ 7 ║   7   │  14  │  21  │  28  │  35  │  42  │  49  │  56  │  63  │
  89. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  90. ║ 8 ║   8   │  16  │  24  │  32  │  40  │  48  │  56  │  64  │  72  │
  91. ╟───╫───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
  92. ║ 9 ║   9   │  18  │  27  │  36  │  45  │  54  │  63  │  72  │  81  │
  93. ╚═══╩───────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┘
  94.  
  95. This means that instead of calculating 9*4, you just find the 9 on the
  96. top and the 4 on the side, and the resulting number is the answer.  This
  97. type of table is very useful when the equations are very long to do.
  98.  
  99. The example I am going to use for this part is that of circles. Cast
  100. your minds back to Part 3 on lines and circles. The circle section took
  101. quite a while to finish drawing, mainly because I had to calculate the
  102. SIN and COS for EVERY SINGLE POINT. Calculating SIN and COS is obviously
  103. very slow, and that was reflected in the speed of the section.
  104.  
  105.  
  106. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  107. ■  How do I generate a lookup table?
  108.  
  109. [Note: the following paragraph contains a significant error.  In the
  110. middle of the paragraph, Denthor says that 360 / 0.4 = 8000.  Last I
  111. checked, 360 / 0.4 = 900.  As a result, I have changed the C++ code to
  112. reflect this.  However, I have left this document fairly unaltered.
  113. So whenever you see a Denthor reference to 8000, you now know why I am
  114. using 900.]
  115.  
  116. This is very simple. In my example, I am drawing a circle. A circle has
  117. 360 degrees, but for greater accuracy, to draw my circle I will start
  118. with zero and increase my degrees by 0.4. This means that in each circle
  119. there need to be 8000 SINs and COSes (360/0.4=8000).  Putting these into 
  120. the base 64k that Pascal allocates for normal variables is obviously not
  121. a happening thing, so we define them as pointers in the following manner: 
  122.  
  123.  [Pascal]
  124.  
  125.         TYPE   table = Array [1..8000] of real;
  126.  
  127.         VAR    sintbl : ^table;
  128.                costbl : ^table;
  129.  
  130.  [C++]
  131.  
  132.         const  TABLESIZE = 900;
  133.  
  134.         float  *costbl=NULL;
  135.         float  *sintbl=NULL;
  136.  
  137. Then in the program we get the memory for these two pointers. Asphyxia 
  138. was originally thinking of calling itself Creative Reboot Inc., mainly 
  139. because we always forgot to get the necessary memory for our pointers.
  140. (Though a bit of creative assembly coding also contributed to this. We
  141. wound up rating our reboots on a scale of 1 to 10 ;-)). The next obvious
  142. step is to place our necessary answers into our lookup tables.  This can
  143. take a bit of time, so in a demo, you would do it in the very beginning
  144. (people just think it's slow disk access or something), or after you
  145. have shown a picture (while the viewer is admiring it, you are
  146. calculating pi to its 37th degree in the background ;-)) Another way of
  147. doing it is, after calculating it once, you save it to a file which you
  148. then load into the variable at the beginning of the program. Anyway,
  149. this is how we will calculate the table for our circle :
  150.  
  151.  [Pascal]
  152.  
  153.     Procedure Setup;
  154.     VAR deg:real;
  155.     BEGIN
  156.       deg:=0;
  157.       for loop1:=1 to 8000 do BEGIN
  158.         deg:=deg+0.4;
  159.         costbl^[loop1]:=cos (rad(deg));
  160.         sintbl^[loop1]:=sin (rad(deg));
  161.       END;
  162.     END;
  163.  
  164.  [C++]  [Note: I have included "Setup" within the LookupCirc() function]
  165.  
  166.       deg = 0.0;
  167.       for (loop1=0;loop1<TABLESIZE;loop1++) {
  168.         costbl[loop1] = cos(rad(deg));
  169.         sintbl[loop1] = sin(rad(deg));
  170.         deg += 0.4;
  171.       }
  172.  
  173. This will calculate the needed 16000 reals and place them into our two
  174. variables. The amount of time this takes is dependant on your computer.
  175.  
  176.  
  177. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  178. ■  How do I use a lookup table?
  179.  
  180. This is very easy. In your program, wherever you put
  181.  
  182.  [Pascal]      cos (rad(deg))
  183.  [C++]         cos (rad(deg))
  184.  
  185. you just replace it with :
  186.  
  187.  [Pascal]      costbl^[deg]
  188.  [C++]         costbl[deg]
  189.  
  190. Easy, no? Note that the new "deg" variable is now an integer, always
  191. between 1 and 8000.
  192.  
  193.  
  194. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  195. ■ Where else do I use lookup tables?
  196.  
  197. Lookup tables may be used in many different ways. For example, when
  198. working out 3-dimensional objects, sin and cos are needed often, and are
  199. best put in a lookup table. In a game, you may pregen the course an
  200. enemy may take when attacking. Even saving a picture (for example, a
  201. plasma screen) after generating it, then loading it up later is a form
  202. of pregeneration.
  203.  
  204. When you feel that your program is going much too slow, your problems
  205. may be totally sorted out by using a table. Or, maybe not. ;-)
  206.  
  207.  
  208. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  209. ■  In closing
  210.  
  211. As you have seen above, lookup tables aren't all that exciting, but they
  212. are useful and you need to know how to use them. The attached sample
  213. program will demonstrate just how big a difference they can make.
  214.  
  215. Keep on coding, and if you finish anything, let me know about it! I
  216. never get any mail, so all mail is greatly appreciated ;-)
  217.  
  218. Sorry, no quote today, it's hot and I'm tired. Maybe next time ;-)
  219.  
  220.   - Denthor
  221.